package com.dankegongyu.lib.common.util;

import android.Manifest;

import com.danke.lib.permission.RxPermissions;

import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2019/1/31 11:10
 */
public class DKPermission {

    /**
     * 这里Keep一下，发现release包有时候这个地方会报错 java.lang.IllegalAccessError
     */
    @Keep
    public interface Callback {
        @Keep
        void onResult(boolean granted);
    }

    public static void request(final FragmentActivity activity, final String[] permissions, @Nullable final Callback callback) {
        if (activity == null || permissions == null || permissions.length == 0) {
            return;
        }
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 必须在主线程调用
                new RxPermissions(activity)
                        .request(permissions)
                        .subscribe(new PermissionObserver(callback));
            }
        });
    }

    public static void request(final Fragment fragment, final String[] permissions, @Nullable final Callback callback) {
        if (fragment == null || fragment.getActivity() == null || permissions == null || permissions.length == 0) {
            return;
        }
        fragment.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // 必须在主线程调用
                new RxPermissions(fragment)
                        .request(permissions)
                        .subscribe(new PermissionObserver(callback));
            }
        });
    }

    private static class PermissionObserver implements Observer<Boolean> {
        private Callback mCallback;

        PermissionObserver(Callback callback) {
            mCallback = callback;
        }

        @Override
        public void onSubscribe(Disposable d) {

        }

        @Override
        public void onNext(Boolean o) {
//                        DKLog.e("onNext: " + aBoolean);
            if (mCallback != null) {
                mCallback.onResult(o);
            }
        }

        @Override
        public void onError(Throwable e) {
//                        DKLog.e("onError");
            if (mCallback != null) {
                mCallback.onResult(false);
            }
        }

        @Override
        public void onComplete() {
//                        DKLog.e("onComplete");
        }
    }


    /**
     * 请求定位权限
     */
    public static void requestLocationPermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
        request(activity, permissions, callback);
    }

    /**
     * 请求定位权限
     */
    public static void requestLocationPermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION};
        request(fragment, permissions, callback);
    }


    /**
     * 请求文件存储权限，主要用于：SD卡存储和读取文件等
     */
    public static void requestStoragePermissions(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(activity, permissions, callback);
    }

    /**
     * 请求文件存储权限，主要用于：SD卡存储和读取文件等
     */
    public static void requestStoragePermissions(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求摄像头权限
     */
    public static void requestCameraPermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(activity, permissions, callback);
    }

    /**
     * 请求摄像头权限
     */
    public static void requestCameraPermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求录音权限
     */
    public static void requestRecordAudioPermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(activity, permissions, callback);
    }

    /**
     * 请求录音权限
     */
    public static void requestRecordAudioPermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求录制视频权限
     */
    public static void requestRecordVideoPermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(activity, permissions, callback);
    }

    /**
     * 请求录制视频权限
     */
    public static void requestRecordVideoPermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO,
                Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求手机信息权限
     */
    public static void requestPhoneStatePermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.READ_PHONE_STATE};
        request(activity, permissions, callback);
    }

    /**
     * 请求手机信息权限
     */
    public static void requestPhoneStatePermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.READ_PHONE_STATE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求拨号权限
     */
    public static void requestCallPhonePermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CALL_PHONE};
        request(activity, permissions, callback);
    }

    /**
     * 请求拨号权限
     */
    public static void requestCallPhonePermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.CALL_PHONE};
        request(fragment, permissions, callback);
    }


    /**
     * 请求通讯录权限
     */
    public static void requestContactsPermission(FragmentActivity activity, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS};
        request(activity, permissions, callback);
    }

    /**
     * 请求通讯录权限
     */
    public static void requestContactsPermission(Fragment fragment, @Nullable Callback callback) {
        String[] permissions = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS};
        request(fragment, permissions, callback);
    }
}
